package com.heima.wemedia.service.admin;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.admin.dtos.NewsAuthDto;
import com.heima.model.admin.vo.NewsVo;
import com.heima.model.admin.vo.WmNewsUserVO;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.constants.CommonConstants;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmNewsFeignMapper;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.WmNewsFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class WmNewsFeignServiceImpl implements WmNewsFeignService {

    @Autowired
    private WmNewsFeignMapper feignMapper;
    @Autowired
    private WmChannelMapper wmChannelMapper;
    @Autowired
    private WmUserMapper wmUserMapper;


    /**
     *  文章 分页查询
     * @param dto
     * @return
     */
    @Override
    public ResponseResult listNews(NewsAuthDto dto) {
        dto.checkParam();
        // 计算 offset
        int offset = (dto.getPage() - 1) * dto.getSize();
        // 设置到 DTO（如果 Mapper 直接用 DTO）
        dto.setPage(offset);

        log.info("业务逻辑层开始执行查询语句");
        List<NewsVo> newsVoList = feignMapper.listNews(dto);
        log.info("查询结果返回：{}",newsVoList);
        return ResponseResult.okResult(feignMapper.listNews(dto));
    }


    //查看文章详情
    @Override
    public ResponseResult getNewsDetail(Integer id) {
        WmNewsUserVO wmNewsUserVO = feignMapper.getNewsDetail(id);
        log.info("查询到的详情返回：{}", wmNewsUserVO);
        return ResponseResult.okResult(wmNewsUserVO);
    }


    //文章人工审核 拒绝
    @Override
    public ResponseResult auth_fail(NewsAuthDto dto) {
        log.info("业务逻辑层开始处理人工拒绝申请");
        dto.setStatus(CommonConstants.STATUS_FAILED);
        log.info("审核失败更新后的文章：{}", dto);
        feignMapper.auth_fail(dto);
        return ResponseResult.okResult(dto.getMsg());

    }

    //文章人工审核 通过 改状态
    @Override
    public ApArticle authPass(NewsAuthDto dto) {
        log.info("业务逻辑层开始工审核通过并改状态");
        //1.修改表 文章中的状态
        dto.setStatus(CommonConstants.STATUS_APPROVED);
        feignMapper.upde(dto);
        //2.拿到修改后WmNews 返回给前端
        WmNews wmNews = feignMapper.selectById(dto.getId());
        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(wmNews, apArticle);
        WmChannel wmChannel = wmChannelMapper.selectById(apArticle.getChannelId());
        apArticle.setAuthorName(wmChannel.getName());
        WmUser wmUser = wmUserMapper.selectById(wmNews.getUserId());
        apArticle.setAuthorName(wmUser.getNickname());
        apArticle.setLabels(String.valueOf(wmNews.getType()));
        apArticle.setPublishTime(new Date());
        apArticle.setCreatedTime(new Date());
        log.info("审核成功都的文章：{}返回给admin", apArticle);
        return apArticle;

    }




    //人工审核 用户通过 新增自媒体用户
    @Override
    public ResponseResult addWmUser(ApUser apUser) {
        if (apUser == null || apUser.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "用户信息不完整");
        }

        log.info("人工审核通过，新增自媒体用户：{}", apUser);

        WmUser wmUser = new WmUser();
        BeanUtils.copyProperties(apUser, wmUser);
        wmUser.setType(ArticleConstants.PERSONAL_ACCOUNT);
        wmUser.setStatus(ArticleConstants.AVAILABLE);
        wmUser.setApUserId(apUser.getId());

        try {
            wmUserMapper.insert(wmUser);
        } catch (Exception e) {
            log.error("新增自媒体用户失败：{}", e.getMessage());
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "用户已存在或数据库异常");
        }

        return ResponseResult.okResult("自媒体用户新增成功");
    }

}